<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>协程实现之切换 | 协程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter6/6_0.html" />
    
    
    <link rel="prev" href="../chapter4/4_0.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5"
        data-chapter-title="协程实现之切换"
        data-filepath="chapter5/5_0.md"
        data-basepath=".."
        data-revision="Sat Oct 13 2018 10:54:16 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="http://www.0voice.com" target="blank" class="custom-link">ntyTcp</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        前言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/1_0.html">
            
                
                    <a href="../chapter1/1_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        协程的起源
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/2_0.html">
            
                
                    <a href="../chapter2/2_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        协程的案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/3_0.html">
            
                
                    <a href="../chapter3/3_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        协程的实现之工作原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/4_0.html">
            
                
                    <a href="../chapter4/4_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        协程的实现之原语操作
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5" data-path="chapter5/5_0.html">
            
                
                    <a href="../chapter5/5_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        协程实现之切换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/6_0.html">
            
                
                    <a href="../chapter6/6_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        协程实现之定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/7_0.html">
            
                
                    <a href="../chapter7/7_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        协程实现之调度器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter8/8_0.html">
            
                
                    <a href="../chapter8/8_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        协程性能测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter9/9_0.html">
            
                
                    <a href="../chapter9/9_0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        协程多核模式
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >协程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="5&#x534F;&#x7A0B;&#x5B9E;&#x73B0;&#x4E4B;&#x5207;&#x6362;">5.&#x534F;&#x7A0B;&#x5B9E;&#x73B0;&#x4E4B;&#x5207;&#x6362;</h1>
<p>&#x2003;&#x2003;<strong>&#x95EE;&#x9898;&#xFF1A;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5982;&#x4F55;&#x5207;&#x6362;&#xFF1F;&#x5207;&#x6362;&#x4EE3;&#x7801;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#xFF1F;</strong>
&#x2003;&#x2003;&#x9996;&#x5148;&#x6765;&#x56DE;&#x987E;&#x4E00;&#x4E0B;x86_64&#x5BC4;&#x5B58;&#x5668;&#x7684;&#x76F8;&#x5173;&#x77E5;&#x8BC6;&#x3002;x86_64 &#x7684;&#x5BC4;&#x5B58;&#x5668;&#x6709;16&#x4E2A;64&#x4F4D;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x5206;&#x522B;&#x662F;&#xFF1A;%rax, %rbx, %rcx, %esi, %edi, %rbp, %rsp, %r8, %r9, %r10, %r11, %r12,
&#x2003;&#x2003;%r13, %r14, %r15&#x3002;
&#x2003;&#x2003;%rax &#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x4F7F;&#x7528;&#x7684;&#x3002;
&#x2003;&#x2003;%rsp &#x6808;&#x6307;&#x9488;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x6307;&#x5411;&#x6808;&#x9876;
&#x2003;&#x2003;%rdi, %rsi, %rdx, %rcx, %r8, %r9 &#x7528;&#x4F5C;&#x51FD;&#x6570;&#x53C2;&#x6570;&#xFF0C;&#x4F9D;&#x6B21;&#x5BF9;&#x5E94;&#x7B2C;1&#x53C2;&#x6570;&#xFF0C;&#x7B2C;2&#x53C2;&#x6570;&#x3002;&#x3002;&#x3002;
&#x2003;&#x2003;%rbx, %rbp, %r12, %r13, %r14, %r15 &#x7528;&#x4F5C;&#x6570;&#x636E;&#x5B58;&#x50A8;&#xFF0C;&#x9075;&#x5FAA;&#x8C03;&#x7528;&#x8005;&#x4F7F;&#x7528;&#x89C4;&#x5219;&#xFF0C;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x5C31;&#x662F;&#x968F;&#x4FBF;&#x7528;&#x3002;&#x8C03;&#x7528;&#x5B50;&#x51FD;&#x6570;&#x4E4B;&#x524D;&#x8981;&#x5907;&#x4EFD;&#x5B83;&#xFF0C;&#x4EE5;&#x9632;&#x5B83;&#x88AB;&#x4FEE;&#x6539;
&#x2003;&#x2003;%r10, %r11 &#x7528;&#x4F5C;&#x6570;&#x636E;&#x5B58;&#x50A8;&#xFF0C;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x524D;&#x8981;&#x5148;&#x4FDD;&#x5B58;&#x539F;&#x503C;&#x3002;</p>
<p>&#x2003;&#x2003;&#x4E0A;&#x4E0B;&#x6587;&#x5207;&#x6362;&#xFF0C;&#x5C31;&#x662F;&#x5C06;CPU&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x6682;&#x65F6;&#x4FDD;&#x5B58;&#xFF0C;&#x518D;&#x5C06;&#x5373;&#x5C06;&#x8FD0;&#x884C;&#x7684;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5BC4;&#x5B58;&#x5668;&#xFF0C;&#x5206;&#x522B;mov&#x5230;&#x76F8;&#x5BF9;&#x5E94;&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x4E0A;&#x3002;&#x6B64;&#x65F6;&#x4E0A;&#x4E0B;&#x6587;&#x5B8C;&#x6210;&#x5207;&#x6362;&#x3002;&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#xFF1A;<br><img src="../image/6_1.png" alt="&#x534F;&#x7A0B;&#x5207;&#x6362;&#x4E4B;CPU&#x5BC4;&#x5B58;&#x5668;&#x64CD;&#x4F5C;&#x56FE;"><br>&#x2003;&#x2003;&#x5207;&#x6362;_switch&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#xFF1A;<br>&#x2003;&#x2003;int _switch(nty_cpu_ctx <em>new_ctx, nty_cpu_ctx </em>cur_ctx);
&#x2003;&#x2003;&#x53C2;&#x6570;1&#xFF1A;&#x5373;&#x5C06;&#x8FD0;&#x884C;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x5BC4;&#x5B58;&#x5668;&#x5217;&#x8868;
&#x2003;&#x2003;&#x53C2;&#x6570;2&#xFF1A;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x5BC4;&#x5B58;&#x5668;&#x5217;&#x8868;
&#x2003;&#x2003;&#x6211;&#x4EEC;nty_cpu_ctx&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x4E3A;&#x4E86;&#x517C;&#x5BB9;x86&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#x9879;&#x547D;&#x4EE4;&#x91C7;&#x7528;&#x7684;&#x662F;x86&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x540D;&#x5B57;&#x547D;&#x540D;&#x3002;</p>
<pre><code>typedef struct _nty_cpu_ctx {
    void *esp; //
    void *ebp;
    void *eip;
    void *edi;
    void *esi;
    void *ebx;
    void *r1;
    void *r2;
    void *r3;
    void *r4;
    void *r5;
} nty_cpu_ctx;
</code></pre><p>&#x2003;&#x2003; _switch&#x8FD4;&#x56DE;&#x540E;&#xFF0C;&#x6267;&#x884C;&#x5373;&#x5C06;&#x8FD0;&#x884C;&#x534F;&#x7A0B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x3002;&#x662F;&#x5B9E;&#x73B0;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x5207;&#x6362;  </p>
<pre><code>0: __asm__ (
1: &quot;    .text                                  \n&quot;
2: &quot;       .p2align 4,,15                                   \n&quot;
3: &quot;.globl _switch                                          \n&quot;
4: &quot;.globl __switch                                         \n&quot;
5: &quot;_switch:                                                \n&quot;
6: &quot;__switch:                                               \n&quot;
7: &quot;       movq %rsp, 0(%rsi)      # save stack_pointer     \n&quot;
8: &quot;       movq %rbp, 8(%rsi)      # save frame_pointer     \n&quot;
9: &quot;       movq (%rsp), %rax       # save insn_pointer      \n&quot;
10: &quot;       movq %rax, 16(%rsi)                              \n&quot;
11: &quot;       movq %rbx, 24(%rsi)     # save rbx,r12-r15       \n&quot;
12: &quot;       movq %r12, 32(%rsi)                              \n&quot;
13: &quot;       movq %r13, 40(%rsi)                              \n&quot;
14: &quot;       movq %r14, 48(%rsi)                              \n&quot;
15: &quot;       movq %r15, 56(%rsi)                              \n&quot;
16: &quot;       movq 56(%rdi), %r15                              \n&quot;
17: &quot;       movq 48(%rdi), %r14                              \n&quot;
18: &quot;       movq 40(%rdi), %r13     # restore rbx,r12-r15    \n&quot;
19: &quot;       movq 32(%rdi), %r12                              \n&quot;
20: &quot;       movq 24(%rdi), %rbx                              \n&quot;
21: &quot;       movq 8(%rdi), %rbp      # restore frame_pointer  \n&quot;
22: &quot;       movq 0(%rdi), %rsp      # restore stack_pointer  \n&quot;
23: &quot;       movq 16(%rdi), %rax     # restore insn_pointer   \n&quot;
24: &quot;       movq %rax, (%rsp)                                \n&quot;
25: &quot;       ret                                              \n&quot;
26: );
</code></pre><p>&#x2003;&#x2003;&#x6309;&#x7167;x86_64&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x5B9A;&#x4E49;&#xFF0C;%rdi&#x4FDD;&#x5B58;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x503C;&#xFF0C;&#x5373;new_ctx&#x7684;&#x503C;&#xFF0C;%rsi&#x4FDD;&#x5B58;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x503C;&#xFF0C;&#x5373;&#x4FDD;&#x5B58;cur_ctx&#x7684;&#x503C;&#x3002;X86_64&#x6BCF;&#x4E2A;&#x5BC4;&#x5B58;&#x5668;&#x662F;64bit&#xFF0C;8byte&#x3002;<br>&#x2003;&#x2003;Movq %rsp, 0(%rsi) &#x4FDD;&#x5B58;&#x5728;&#x6808;&#x6307;&#x9488;&#x5230;cur_ctx&#x5B9E;&#x4F8B;&#x7684;rsp&#x9879;<br>&#x2003;&#x2003;Movq %rbp, 8(%rsi)<br>&#x2003;&#x2003;Movq (%rsp), %rax  #&#x5C06;&#x6808;&#x9876;&#x5730;&#x5740;&#x91CC;&#x9762;&#x7684;&#x503C;&#x5B58;&#x50A8;&#x5230;rax&#x5BC4;&#x5B58;&#x5668;&#x4E2D;&#x3002;Ret&#x540E;&#x51FA;&#x6808;&#xFF0C;&#x6267;&#x884C;&#x6808;&#x9876;<br>&#x2003;&#x2003;Movq %rbp, 8(%rsi) #&#x540E;&#x7EED;&#x7684;&#x6307;&#x4EE4;&#x90FD;&#x662F;&#x7528;&#x6765;&#x4FDD;&#x5B58;CPU&#x7684;&#x5BC4;&#x5B58;&#x5668;&#x5230;new_ctx&#x7684;&#x6BCF;&#x4E00;&#x9879;&#x4E2D;<br>&#x2003;&#x2003;Movq 8(%rdi), %rbp #&#x5C06;new_ctx&#x7684;&#x503C;<br>&#x2003;&#x2003;Movq 16(%rdi), %rax #&#x5C06;&#x6307;&#x4EE4;&#x6307;&#x9488;rip&#x7684;&#x503C;&#x5B58;&#x50A8;&#x5230;rax&#x4E2D;
&#x2003;&#x2003;Movq %rax, (%rsp) # &#x5C06;&#x5B58;&#x50A8;&#x7684;rip&#x503C;&#x7684;rax&#x5BC4;&#x5B58;&#x5668;&#x8D4B;&#x503C;&#x7ED9;&#x6808;&#x6307;&#x9488;&#x7684;&#x5730;&#x5740;&#x7684;&#x503C;&#x3002;<br>&#x2003;&#x2003;Ret # &#x51FA;&#x6808;&#xFF0C;&#x56DE;&#x5230;&#x6808;&#x6307;&#x9488;&#xFF0C;&#x6267;&#x884C;rip&#x6307;&#x5411;&#x7684;&#x6307;&#x4EE4;&#x3002;<br>&#x2003;&#x2003;&#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;&#x7684;&#x5207;&#x6362;&#x5B8C;&#x6210;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter4/4_0.html" class="navigation navigation-prev " aria-label="Previous page: 协程的实现之原语操作"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter6/6_0.html" class="navigation navigation-next " aria-label="Next page: 协程实现之定义"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"theme-default":{"showLevel":false},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
